﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using digit.Core;
using SMS.ServiceManager;
using SMS.UserManager;

public partial class Modules_ListServiceUpdateLog : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!ClientState.IsAuthenticated)
        {
            Response.Redirect("~/Login.aspx?session=timeout");
        }

        this.lnkDelete.Attributes.Add("onclick", "return confirm('Bạn đã chắc chắn xóa?');");
        this.lnkDeleteBottom.Attributes.Add("onclick", "return confirm('Bạn đã chắc chắn xóa?');");

        if (ClientState.MemberInfo.UserType != 0)
        {
            lnkDelete.Enabled = false;
            lnkDeleteBottom.Enabled = false;
        }
        else
        {
            lnkDelete.Enabled = true;
            lnkDeleteBottom.Enabled = true;

            this.BindUser();
            this.BindData();
        }
    }

    private void BindUser()
    {
        this.ddlUser.DataSource = MembersManager.SelectAll();
        this.ddlUser.DataBind();

        ListItem item = new ListItem("0", "-- Chọn người dùng --");
        this.ddlUser.Items.Add(item);
    }

    protected void lnkDelete_Click(object sender, EventArgs e)
    {
        int iDeleted = 0;

        //Search all checked rows to delete
        foreach (GridViewRow r in this.grvServiceUpdate.Rows)
        {
            //If checkbox is not checked then quit
            HtmlInputCheckBox cbx = (HtmlInputCheckBox)r.Cells[0].Controls[1];
            if (!cbx.Checked) continue;

            //If checkbox is checked then delete
            if (ActivitiesManager.Delete(Util.ToInteger(cbx.Value)))
                iDeleted++;
        }

        //Display result of delete process
        if (iDeleted == 0)
        {
            this.lblError.Text = "Không có bản ghi nào được xóa!";
            return;
        }

        this.lblError.Text = "Đã xóa " + iDeleted.ToString() + " bản ghi!";

        this.BindData();
    }

    protected void grvServiceUpdate_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.grvServiceUpdate.PageIndex = e.NewPageIndex;
        this.BindData();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindData();
    }

    private void BindData()
    {
        try
        {
            DataTable dt = ActivitiesManager.ListBy(this.ddlTable.SelectedValue, int.Parse(this.ddlUser.SelectedValue));

            if (dt != null && dt.Rows.Count > 0)
            {
                this.grvServiceUpdate.DataSource = dt;
                this.grvServiceUpdate.DataBind();
                this.lblError.Text = "Hệ thống tìm thấy tổng số <b>" + dt.Rows.Count + "</b> bản ghi log";
            }
            else
            {
                lblError.Text = "Hiện tại chưa có ghi nhận về log";
            }
        }
        catch { }
    }
}
